<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch, ref, unref, nextTick } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { useI18n } from '@/hooks/web/useI18n'
import { ElTree, ElCheckboxGroup, ElCheckbox } from 'element-plus'
import { saveDictItem, updateDictItem } from '@/api/dict'
import { filter, eachTree } from '@/utils/tree'
import { findIndex } from '@/utils'

const { t } = useI18n()

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  },
  itemCurrentRow: {
    type: Object as PropType<any>,
    default: () => null
  }
})

const treeRef = ref<typeof ElTree>()

const formSchema = ref<FormSchema[]>([
  {
    field: 'itemText',
    label: t('dict.itemText'),
    component: 'Input',
    colProps: {
      span: 24
    }
  },
  {
    field: 'itemValue',
    label: t('dict.itemValue'),
    component: 'Input',
    colProps: {
      span: 24
    }
  },
  {
    field: 'sortOrder',
    label: t('dict.sortOrder'),
    component: 'Input',
    componentProps: {
      type: 'number'
    },
    colProps: {
      span: 24
    }
  },
  {
    field: 'status',
    label: t('userDemo.status'),
    component: 'Select',
    componentProps: {
      options: [
        {
          value: '0',
          label: t('userDemo.disable')
        },
        {
          value: '1',
          label: t('userDemo.enable')
        }
      ]
    },
    value: '1'
  },
  {
    field: 'description',
    label: t('userDemo.remark'),
    component: 'Input',
    colProps: {
      span: 24
    }
  }
])

const rules = reactive({
  dictName: [required()],
  dictCode: [required()]
})

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods

const treeData = ref([])

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    console.log(' props.currentRow', props.currentRow)
    formData.dictId = props.currentRow.id
    formData.dictCode = props.currentRow.dictCode
    const res = await saveDictItem(formData)
    if (!res.success) {
      return
    }
    console.log(formData)
    return formData
  }
}

const updateData = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    formData.dictId = props.currentRow.id
    formData.dictCode = props.currentRow.dictCode
    const res = await updateDictItem(formData)
    if (!res.success) {
      return
    }
    console.log(formData)
    return formData
  }
}

watch(
  () => props.itemCurrentRow,
  (itemCurrentRow) => {
    if (!itemCurrentRow) return
    setValues(itemCurrentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit,
  updateData
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="formSchema" />
</template>
